package com.pubinfo.passbook.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import com.pubinfo.passbook.api.service.ArtificialService;
import com.pubinfo.passbook.common.entity.TPassbook;
import com.pubinfo.passbook.common.entity.TSourceArtificial;
import com.pubinfo.passbook.common.entity.TWiteoffInstance;
import com.pubinfo.passbook.common.mapper.TSourceArtificialMapper;
import com.pubinfo.passbook.common.mapper.TSourcePassbookMapper;
import com.pubinfo.passbook.common.mapper.TWiteoffInstanceMapper;
import com.pubinfo.passbook.common.model.bo.witeoff.WiteoffBase;
import com.pubinfo.passbook.common.model.dto.witeoff.MedicineOnJobOrRetireDTO;
import com.pubinfo.passbook.common.model.ruler.PayInformation;
import com.pubinfo.passbook.common.model.vo.rule.updateArtificialSumParam;
import com.pubinfo.passbook.common.model.vo.rule.updatePassbookSumParam;
import com.pubinfo.passbook.common.service.master.TPassbookService;
import com.pubinfo.passbook.common.service.master.TWiteoffInstanceService;
import com.pubinfo.passbook.common.service.master.TWiteoffInvoiceInfoService;
import com.pubinfo.passbook.common.service.mss.HttpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class ArtificialServiceImpl implements ArtificialService {

    @Autowired
    TWiteoffInstanceMapper tWiteoffInstanceMapper;
    @Autowired
    TWiteoffInstanceService tWiteoffInstanceService;
    @Autowired
    TPassbookService passbookService;
    @Autowired
    TSourcePassbookMapper tSourcePassbookMapper;
    @Autowired
    TWiteoffInvoiceInfoService tWiteoffInvoiceInfoService;
    @Autowired
    HttpService httpService;
    @Autowired
    TSourceArtificialMapper tSourceArtificialMapper;


    @Override
    public Boolean updateInstanceSumById(BigDecimal sum, String instanceId,String acct) throws IOException {
        TWiteoffInstance tWiteoffInstance = tWiteoffInstanceService.getTwiteoffInstanceByOtherSystemMainId(instanceId);
        JSONObject mainXml = tWiteoffInstance.getJson();
        MedicineOnJobOrRetireDTO jobDTO = JSONObject.toJavaObject(mainXml, MedicineOnJobOrRetireDTO.class);
        XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        jobDTO.setSum(sum);
        jobDTO.setIsProcessShort(false);
        if(CollectionUtils.isNotEmpty(jobDTO.getWiteoffs()))
        {
            jobDTO.getWiteoffs().get(0).setSum(sum);
        }
        if(CollectionUtils.isNotEmpty(jobDTO.getLineItems())) {
            jobDTO.getLineItems().get(0).setSum(sum);
        }
        if(CollectionUtils.isNotEmpty(jobDTO.getLineItems())) {
            jobDTO.getLineItems().get(0).setPriceSum(sum);
        }
        if(CollectionUtils.isNotEmpty(jobDTO.getPayMentItems())) {
            jobDTO.getPayMentItems().get(0).setSum(sum.doubleValue());
        }
        if (jobDTO.getOutpatientDepartmentSum().compareTo(BigDecimal.ZERO) != 0) {
            jobDTO.setOutpatientDepartmentSum(sum);
        } else {
            jobDTO.setHospitalizationSum(sum);
        }
        setPassbookSum(jobDTO);
        jobDTO.setOtherSystemMainId(instanceId);
        httpService.updateWiteoffInstance(jobDTO,tWiteoffInstance.getId());

        return true;
    }

    @Override
    public Boolean updateArtificialSumById(updateArtificialSumParam updateArtificialSumParam, String acct) throws IOException {
        if(updateArtificialSumParam.getAmount()==null)updateArtificialSumParam.setAmount(BigDecimal.ZERO);
        TSourceArtificial tSourceArtificial=new TSourceArtificial();
        tSourceArtificial.setApprovedAmount(updateArtificialSumParam.getAmount());
        tSourceArtificial.setId(updateArtificialSumParam.getArtificialId());
        tSourceArtificialMapper.updateArtificialSumById(tSourceArtificial,updateArtificialSumParam.getInstanceId());
        BigDecimal sum=witeoffSum(updateArtificialSumParam.getInstanceId());
        return  updateInstanceSumById(sum,updateArtificialSumParam.getInstanceId(),acct);
    }


    public void setPassbookSum(WiteoffBase jobDTO) {
        List<TPassbook> tPassbooks = passbookService.getPassbookById(jobDTO.getInvoiceIds());
        if (!CollectionUtils.isEmpty(tPassbooks)) {
            passbookService.updateBatchById(tPassbooks);
        }
    }


    @Override
    public Boolean updatePassbookSumById(updatePassbookSumParam updatePassbookSumParam,String acct) throws IOException {
        TWiteoffInstance tWiteoffInstance = tWiteoffInstanceService.getTwiteoffInstanceByOtherSystemMainId(updatePassbookSumParam.getInstanceId());
        if(updatePassbookSumParam.getOriginalAmount()==null)updatePassbookSumParam.setOriginalAmount(BigDecimal.ZERO);
        PayInformation payInformation=new PayInformation();
        payInformation.setReimbursableSum(updatePassbookSumParam.getAmount());
        payInformation.setEInvoiceId(updatePassbookSumParam.getPassbookId());
        tSourcePassbookMapper.updateReimbursableSumByWiteoffId(payInformation,tWiteoffInstance.getId());
        BigDecimal sum=witeoffSum(updatePassbookSumParam.getInstanceId());
        return  updateInstanceSumById(sum,updatePassbookSumParam.getInstanceId(),acct);
    }

    private BigDecimal witeoffSum(String otherSystemMainId)
    {
        List<TPassbook> passbooks=tSourcePassbookMapper.selectPassbookByOtherSystemMainId(otherSystemMainId);
        List<TSourceArtificial> artificialList=tSourceArtificialMapper.selectArtificialByOtherSystemMainId(otherSystemMainId);
        BigDecimal sum=BigDecimal.ZERO;
        for(TPassbook tPassbook:passbooks)
        {
            sum=sum.add(tPassbook.getReimbursableSum());
        }
        for(TSourceArtificial tSourceArtificial:artificialList)
        {
            sum=sum.add(tSourceArtificial.getApprovedAmount());
        }
        return sum;
    }
}
